--[[
CardUtil
]]
local SKILL_CardConfig = require("app.data.skill.SKILL_CardConfig")
local SKILL_SkillConfig = require("app.data.skill.SKILL_SkillConfig")
local CardUtil = {};





CardUtil.row =  7 ;--x 遍历 7
CardUtil.col =  6 ;--y遍历 6

    
    
CardUtil.width = 85;
CardUtil.height = 85;




CardUtil.id_Obstacle = 20;--障碍物

--三种触发类型
CardUtil.triggerType_eliminate = 1;--消除类型
CardUtil.triggerType_pet = 2;--宠物技能
CardUtil.triggerType_item = 3;--道具技能






--[[
通过卡牌的x，y索引 
 获取卡牌的真实坐标位置
]]
function CardUtil.getCardPositionByIndex(xIndex,yIndex)
	if checkint(xIndex) == 0 and checkint(yIndex) == 0 then 
		return display.cx,display.top - 112--CardUtil.height ;
	end
	
	local x = (xIndex-1) * CardUtil.width + CardUtil.width/2 + 20 ;
	local y = (yIndex-1) * CardUtil.height + CardUtil.height/2 + 15  + 200;
	
	
	return x,y;
end








----------------消除技能---------------------------------------


--[[
释放技能
]]
function CardUtil.dispatchOneSkill(model)


end

function CardUtil.dispatchSkill(model,allCleanCard,triggerType,onEndComplete)
	 local newCleanCard = {};
	 
	 for i,id in pairs(allCleanCard) do 
		local targetCard = model:getObject(id);
		local previousCardSpriteId = targetCard:getPreviousCardSpriteId();
		
		--判断是否含有技能
		local skillCard = SKILL_CardConfig[previousCardSpriteId];
		if skillCard  then --含有技能
			local skillId = skillCard.own_skill_id
			if  skillId ~= 0 then
				
				--开始释放技能 start  ============
				
					local skillConfig = SKILL_SkillConfig[skillId];
					echoj("技能：",skillConfig);
					if skillConfig.skill_triggerType == triggerType then --当前触发类型一致
						--技能类型(1=消除类型，2=联锁闪电类型(消除手上相同)，
						--3=联锁闪电类型(消除手上不同因子颜色)，4=同化(将手上相同因子颜色),
						--5=障碍物消除，6时间倒流（时间可以增加也可以减少）)

						if skillConfig.skill_type == 1 then 
							local currentCleanCard = CardUtil.eliminateByVH(model,targetCard,skillConfig.eliminate_row,skillConfig.eliminate_col)
							CardEliminateAll.eliminateCard(model,currentCleanCard)  
							
							table.insertOneArr(newCleanCard,currentCleanCard)
						elseif skillConfig.skill_type == 2 then 
							local currentCleanCard = CardUtil.eliminateByColor(model,targetCard,previousCardSpriteId,skillConfig.eliminate_row,skillConfig.eliminate_col,true)
							CardEliminateAll.eliminateCard(model,currentCleanCard)  
							
							table.insertOneArr(newCleanCard,currentCleanCard)
						elseif skillConfig.skill_type == 3 then 
							local currentCleanCard = CardUtil.eliminateByColor(model,targetCard,previousCardSpriteId,skillConfig.eliminate_row,skillConfig.eliminate_col)
							CardEliminateAll.eliminateCard(model,currentCleanCard)  
							
							table.insertOneArr(newCleanCard,currentCleanCard)
						elseif skillConfig.skill_type == 4 then 
							CardUtil.changeByColor(model,targetCard,previousCardSpriteId,skillConfig.eliminate_row,skillConfig.eliminate_col)
						elseif skillConfig.skill_type == 5 then 
							local currentCleanCard =CardUtil.eliminateByObstacle(model,targetCard,skillConfig.eliminate_row,skillConfig.eliminate_col)
							CardEliminateAll.eliminateCard(model,currentCleanCard)  
							
							--table.insertOneArr(newCleanCard,currentCleanCard)
						elseif skillConfig.skill_type == 6 then 
							model.fightAccount_:setEnergyFullNums(skillConfig.time)
						end
					
					end
					
				--开始释放技能 end	============
				
			end
			
			if skillCard.isObstacle then --是否是障碍物
			
			end
		
		end
		
	end
	
	
	
	
	
	--有新的卡牌
	if #newCleanCard > 0 then
		CardUtil.dispatchSkill(model,newCleanCard,CardUtil.triggerType_eliminate,onEndComplete)
	else
		if onEndComplete then onEndComplete() end
	end
end








local function getMinMaxXYByWH(targetCard,vertical,horizontal)
	local targetxIndex,targetyIndex = targetCard:getXYIndexById();
	local minX = targetxIndex - vertical; if minX <1 then minX = 1 end
	local minY = targetyIndex - horizontal;if minY <1 then minY = 1 end
	
	local maxX = targetxIndex + vertical;if maxX >CardUtil.row then maxX = CardUtil.row end
	local maxY = targetyIndex + horizontal;if maxY >CardUtil.col then maxY = CardUtil.col end
	
	return minX,minY,maxX,maxY;
end



--[[
以targetCard为中心，
清除n行n列
@param vertical
@param horizontal
]]
function CardUtil.eliminateByVH(model,targetCard,vertical,horizontal)
	local minX,minY,maxX,maxY = getMinMaxXYByWH(targetCard,vertical,horizontal)
	
	local cleanCard ={};--返回的重组卡牌
	
	echoj(minX,minY,maxX,maxY);
	for i = minY,maxY,1 do  --y遍历
		for j = minX,maxX,1 do
			local id = "static:"..j.."_"..i;
			if id ~= targetCard.id_ then
				local currentCard = model:getObject(id);
				local cardSpriteId = currentCard:getCardSpriteId();
				
				if cardSpriteId ~= 0  then
					cleanCard[#cleanCard + 1] = id;
				end
			end
		end
	end 
	
	return cleanCard;
end









--[[
以 targetCard 为中心，
清除与 targetCard 相同颜色/不同颜色  的卡牌
@param nums
@param isSame 
]]
function CardUtil.eliminateByColor(model,targetCard,targetCardSpriteId,vertical,horizontal,isSame)
	if targetCardSpriteId == 0 then return {} end
	local minX,minY,maxX,maxY = getMinMaxXYByWH(targetCard,vertical,horizontal)
	
	local cleanCard ={};--返回的重组卡牌
	
	
	for i = minY,maxY,1 do  --y遍历
		for j = minX,maxX,1 do
			local id = "static:"..j.."_"..i;
			local obj = model:getObject(id);
 			local cardSpriteId= obj:getCardSpriteId();
 			
 			if isSame == true then
 				if cardSpriteId == targetCardSpriteId then
					cleanCard[#cleanCard + 1] = id;
				end
			else
				if cardSpriteId ~= targetCardSpriteId then
					cleanCard[#cleanCard + 1] = id;
				end
			end
		end
	end 
	
	return cleanCard;
end

--[[
同化 ：将与手头因子颜色不同的因子染成与手头因子相同的颜色
注意：targetCardSpriteId 不能为0 
]]
function CardUtil.changeByColor(model,targetCard,targetCardSpriteId,vertical,horizontal)
	--可以先调用 CardUtil.eliminateByColor  方法获取到卡片  
	local cleanCard = CardUtil.eliminateByColor(model,targetCard,targetCardSpriteId,vertical,horizontal);
	for i,id in pairs(cleanCard) do 
		local obj = model:getObject(id);
		obj:setCardSpriteId(targetCardSpriteId);
	end
end


--[[
将场上N个障碍物消除。
]]
function CardUtil.eliminateByObstacle(model,targetCard,vertical,horizontal)
	local cards = CardUtil.eliminateByColor(model,targetCard,CardUtil.id_Obstacle,vertical,horizontal,true)
	return cards;
end















--[[
剔除相同的卡片

function CardUtil.flushCommon(cards,comparedCards)
	for k,v in pairs(cards) do 
		for k1,v1 in pairs(comparedCards) do 
			if v1 == v then
				table.remove(cards,k1)
			end
		end
	end
	
	return cards,comparedCards
end
]]

return CardUtil
